﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4_Test.Models;

namespace MVC4_Test.Controllers
{
    public class FavoriteController : Controller
    {
        private StackOverflowContext db = new StackOverflowContext();

        // in de url kan je favoriete vraag oproepen
        // GET: /Favorite/

        public ActionResult Index()
        {
            var favorites = db.Favorites.Include(f => f.Question);
            return View(favorites.ToList());
        }

        // hierbij met de details
        // GET: /Favorite/Details/5

        public ActionResult Details(int id = 0)
        {
            Favorite favorite = db.Favorites.Find(id);
            if (favorite == null)
            {
                return HttpNotFound();
            }
            return View(favorite);
        }

        // een favoriet vraag crearen 
        // GET: /Favorite/Create

        public ActionResult Create()
        {
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text");
            return View();
        }

        // als het valid save het dan
        // POST: /Favorite/Create

        [HttpPost]
        public ActionResult Create(Favorite favorite)
        {
            if (ModelState.IsValid)
            {
                db.Favorites.Add(favorite);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text", favorite.QuestionId);
            return View(favorite);
        }

        // werkt over het algemeen hetzelfde als answers maar dan met een ander model
        // GET: /Favorite/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Favorite favorite = db.Favorites.Find(id);
            if (favorite == null)
            {
                return HttpNotFound();
            }
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text", favorite.QuestionId);
            return View(favorite);
        }

        //
        // POST: /Favorite/Edit/5

        [HttpPost]
        public ActionResult Edit(Favorite favorite)
        {
            if (ModelState.IsValid)
            {
                db.Entry(favorite).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "Text", favorite.QuestionId);
            return View(favorite);
        }

        //
        // GET: /Favorite/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Favorite favorite = db.Favorites.Find(id);
            if (favorite == null)
            {
                return HttpNotFound();
            }
            return View(favorite);
        }

        //
        // POST: /Favorite/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Favorite favorite = db.Favorites.Find(id);
            db.Favorites.Remove(favorite);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}